package com.centaline.androidsalesblog.utils;

import android.annotation.SuppressLint;
import android.content.Context;

import com.centaline.androidsalesblog.constants.AppConstant;

import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * JPush工具
 * Created by Lihq on 2015/9/16.
 */
@SuppressLint("DefaultLocale")
public class JPushExampleUtil {
    public static boolean isValidTagAndAlias(String s) {
        Pattern p = Pattern.compile("^[\\u4E00-\\u9FA50-9a-zA-Z_-]{0,}$");
        Matcher m = p.matcher(s);
        return m.matches();
    }

    public static void setAlias(Context context) {
        StringBuilder alias = new StringBuilder();
        if (AppConstant.IsLogin) {
            alias.append(String.format(Locale.CHINA, "%s_%s", AppConstant.CityCode, AppConstant.UserId));
        }
        if (isValidTagAndAlias(alias.toString().toLowerCase())) {
            JPushInterface.resumePush(context);
            JPushInterface.setAliasAndTags(context, alias.toString().toLowerCase(), null, new TagAliasCallback() {
                @Override
                public void gotResult(int i, String s, Set<String> set) {

                }
            });
        }
    }
}
